
#ifndef PROJECTZ_NUSER_NCOMMAND_CS_REQ_EQUIP_RELEASE_H_
#define PROJECTZ_NUSER_NCOMMAND_CS_REQ_EQUIP_RELEASE_H_

#include "../../engine.h"

#include "../../User.h"
#include "../../NLogic/Equip.h"
#include "../../NLogic/Item.h"
#include "../../NLogic/Function.h"

namespace ProjectZ { namespace NCommand { namespace NWorld {

class CS_REQ_EQUIP_RELEASE
{
public:
	static void sendFail(User * pUser, uint8_t equip_pos)
	{
		::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
		pAck->Write2(ACK_UNKNOWN_ERROR);
		pAck->Write1(equip_pos);
		pAck->MakePacket(CMD_SC_REQ_EQUIP_RELEASE);
		pUser->Send(pAck);
	}

	static void OnExecute(User * pUser, ::Network::Packet * pPacket)
	{
		PREPARE_PACKET;

		uint8_t equip_pos = 0;
		* pPacket >> equip_pos;

		if (static_cast<EnumItemEquipPosition>(equip_pos) == EnumItemEquipPosition::ITEM_EQUIP_POS_WEAPON)
		{
			DEBUGLOG("%s ITEM_EQUIP_POS_WEAPON", __FUNCTION__);
			sendFail(pUser, equip_pos);
			return;
		}

		if (static_cast<EnumItemEquipPosition>(equip_pos) == EnumItemEquipPosition::ITEM_EQUIP_POS_VEHICLE)
		{
			DEBUGLOG("%s ITEM_EQUIP_POS_VEHICLE", __FUNCTION__);
			sendFail(pUser, equip_pos);
			return;
		}

		if (static_cast<EnumItemEquipPosition>(equip_pos) == EnumItemEquipPosition::ITEM_EQUIP_POS_FAIRY)
		{
			DEBUGLOG("%s ITEM_EQUIP_POS_FAIRY", __FUNCTION__);
			sendFail(pUser, equip_pos);
			return;
		}

		NLogic::Equip * pEquip = pUser->GetEquip();
		if (NULL == pEquip)
		{
			DEBUGLOG("%s NULL == pEquip", __FUNCTION__);
			sendFail(pUser, equip_pos);
			return;
		}

		NLogic::Item * pItem = pEquip->GetItem(static_cast<EnumItemEquipPosition>(equip_pos));
		if (NULL == pItem)
		{
			DEBUGLOG("%s NULL == pItem", __FUNCTION__);
			if (ITEM_EQUIP_POS_NAME_TAG != equip_pos)
			{
				sendFail(pUser, equip_pos);
				return;
			}
		}

		if (true == pUser->GetEquip()->IsExistItem(static_cast<EnumItemEquipPosition>(equip_pos)))
		{
			pUser->GetEquip()->PopItem(static_cast<EnumItemEquipPosition>(equip_pos));
		}

		// 캐릭터 정보 갱신
		pUser->GetEquip()->UpdateCharacterInfo();

		::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
		pAck->Write2(ACK_OK);
		* pAck << equip_pos;
		pAck->MakePacket(CMD_SC_REQ_EQUIP_RELEASE);
		pUser->Send(pAck);

		NLogic::Space * pSpace = pUser->GetSpace();
		if (NULL == pSpace)
			return;

		const NLogic::Space::USERTREE & clsUserTree = pSpace->GetUserTree();
		std::for_each(clsUserTree.begin(), clsUserTree.end(), [&](const std::pair<const int, ProjectZ::User*>& pair) {
			if (NULL != pair.second && pair.first != pUser->GetUserSeq())
			{
				NLogic::NFunction::SendCharacterInfoSimple(pUser, pair.second);
			}
		} );
	}
};

} /*NWorld*/ } /*NCommand*/ } /*ProjectZ*/


#endif /* PROJECTZ_NUSER_NCOMMAND_CS_REQ_EQUIP_RELEASE_H_ */
